;
const jsonpTool = (function () {
    function getParams(obj) {
        var str = "";
        for (var key in obj) {
            str += key + "=" + obj[key] + "&";
        }
        return str;
    }


    //jquery的jsonp就是叫做jsonpCallback
    // {url:xxx,data:{},success:fn,jsonpCallback}
    function getJSONP(opt) {
        return new Promise((resolve, reject) => {

            //1.随机生函数
            var fnName = "jquery_" + Date.now();
            //2.给随机函数绑定 函数
            window[fnName] = opt.success = resolve;
            //3.创建scriot,并绑定src,
            var oScript = document.createElement("script");

            opt.url = opt.url + "?" + getParams(opt.data) + opt.jsonpCallback + "=" + fnName;
            oScript.setAttribute("src", opt.url)
            //4.让srcipt加载数据 (直接挂到body上)
            document.body.appendChild(oScript)
            //5.清除随机函数和script标签
            oScript.addEventListener("load", () => {
                // oScript.remove();
                delete(window[fnName]);
            })
        })
    }
    return {
        getParams,
        getJSONP
    }

})()